package com.qmyx.supply.admin.controller;

import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.fastjson.JSONArray;
import com.kuaidi100.sdk.response.QueryTrackData;
import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.model.dto.ProductDto;
import com.ytjj.qmyx.supplychain.common.model.request.ReturnGoodsRequest;
import com.ytjj.qmyx.supplychain.mapper.ExpressOrdersMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxExpressOrders;
import com.ytjj.qmyx.supplychain.service.ReturnGoodsService;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 
 * 
 * @Version: 1.0
 */
@RestController
@RequestMapping("/returnGoods")
public class ReturnGoodsController {

	@Resource
	private ReturnGoodsService returnGoodsService;
	@Resource
	private ExpressOrdersMapper expressOrdersMapper;

	@PostMapping("/list")
	public CommonResult selectList(@RequestBody ReturnGoodsRequest request) {
		return CommonResult.success(returnGoodsService.selectList(request));
	}

	@GetMapping("/getDetailById")
	public CommonResult getReturnGoodsDetail(@RequestParam Integer id) {
		return CommonResult.success(returnGoodsService.getReturnGoodsDetailInfo(id));
	}

	@GetMapping("/auditSuccess")
	public CommonResult auditSuccess(@RequestParam Integer id) {
		return returnGoodsService.auditSuccess(id);
	}

	@GetMapping("/auditFail")
	public CommonResult auditFail(@RequestParam Integer id, @RequestParam String bankerRemarks) {
		return returnGoodsService.auditFail(id, bankerRemarks);
	}

	@GetMapping("/getLogistics")
	public CommonResult getLogistics(@RequestParam String tel, @RequestParam String expressCompany, @RequestParam String expressNo) {
		if (tel != null && expressCompany != null && expressNo != null) {
			Example example = new Example(YxExpressOrders.class);
			example.createCriteria().andEqualTo("companyNo", expressCompany).andEqualTo("expressEno", expressNo);
			example.orderBy("id").desc();
			List<YxExpressOrders> list = expressOrdersMapper.selectByExample(example);
			if (CollectionUtils.isEmpty(list)) {
				return CommonResult.success("暂无物流信息！");
			}
			List<QueryTrackData> data = JSONArray.parseArray(list.get(0).getInfoText(), QueryTrackData.class);
			return CommonResult.success(data);
		} else {
			return CommonResult.success("暂无物流信息！");
		}
	}

	@PostMapping("/updataAddressById")
	public CommonResult updataBankerAddressById(@RequestBody ProductDto productDto) {
		return CommonResult.success(returnGoodsService.updateAddressIdById(productDto));
	}
}
